package org.seven.jrdp.commons.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public abstract class Excel2007Utils {
	public static final String SUFFIX = ".xlsx";

	public static Workbook getWorkbook(String path) {
		try {
			Workbook workbook = new XSSFWorkbook(path);
			return workbook;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static Workbook getWorkbook(byte[] buf) {
		try {
			InputStream in = new ByteArrayInputStream(buf);
			Workbook workbook = new XSSFWorkbook(in);
			return workbook;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void save(Workbook workbook, String path) {
		try {
			File file = new File(path);
			FileUtils.touch(file);
			OutputStream out = new FileOutputStream(file);
			workbook.write(out);
			out.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void save(Workbook workbook, OutputStream out) {
		try {
			workbook.write(out);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}